package cuongnh.project.japanesediary.vocabulary.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cuongnh.project.japanesediary.vocabulary.model.Sentence;
import cuongnh.project.japanesediary.vocabulary.model.Vocabulary;

@Repository
@Transactional
public class VocabularyDao extends HibernateDaoSupport {
	
	public void save(Vocabulary a1) {
		a1.setCreatedDate(new Date());
		a1.setModifiedDate(new Date());
		getHibernateTemplate().save(a1);
	}

	/**
	 * 'load' returns a proxy with only the id populated. To access members in
	 * the proxy requires a Hibernate session. Haven't figured out how to do
	 * transactions between bundles yet...
	 */
	public Vocabulary load(long id) {
		return (Vocabulary) getHibernateTemplate().load(Vocabulary.class, id);
	}

	public Vocabulary get(long id) {
		return (Vocabulary) getHibernateTemplate().get(Vocabulary.class, id);
	}

	public void update(Vocabulary a1) {
		a1.setModifiedDate(new Date());
		getHibernateTemplate().update(a1);
	}

	public void delete(Vocabulary a1) {
		getHibernateTemplate().delete(a1);
	}

	public Vocabulary merge(Vocabulary a1) {
		a1.setModifiedDate(new Date());
		return (Vocabulary) getHibernateTemplate().merge(a1);
	}
	
	public void saveOrUpdate(Vocabulary a1) {
		a1.setModifiedDate(new Date());
		if(a1.getCreatedDate() == null){
			a1.setCreatedDate(new Date());
		}
		getHibernateTemplate().saveOrUpdate(a1);
	}

	public List<Vocabulary> getAll() {
		return getHibernateTemplate().loadAll(Vocabulary.class);
	}

	public Set<Sentence> getExamples(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyExamples());
		return vocabulary.getVocabularyExamples();
	}
	
	public Vocabulary getVocabularyWithExamples(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,				
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyExamples());
		return vocabulary;
	}
	
	public Vocabulary getVocabularyWithKanjis(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,				
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getKanjis());
		return vocabulary;
	}
	
	public Vocabulary getVocabularyWithGrammars(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getGrammars());
		return vocabulary;
	}
	
	public Vocabulary getVocabularyWithCategories(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyCategories());
		return vocabulary;
	}
	
	public Set<Vocabulary> getRelatedVocabularies(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getRelatedsVocabulary());
		return vocabulary.getRelatedsVocabulary();
	}
	
	public Vocabulary getVocabularyWithRelateds(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getRelatedsVocabulary());
		return vocabulary;
	}
	
	public Set<Vocabulary> getHomophoneVocabularies(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyHomophones());
		return vocabulary.getVocabularyHomophones();
	}
	
	public Vocabulary getVocabularyWithHomophones(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyHomophones());
		return vocabulary;
	}
	
	public Set<Vocabulary> getSynonymVocabularies(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularySynonyms());
		return vocabulary.getVocabularySynonyms();
	}
	
	public Vocabulary getVocabularyWithSynonyms(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularySynonyms());
		return vocabulary;
	}
	
	public Set<Vocabulary> getAntonymVocabularies(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyAntonyms());
		return vocabulary.getVocabularyAntonyms();
	}
	
	public Vocabulary getVocabularyWithAntonyms(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getVocabularyAntonyms());
		return vocabulary;
	}
	
	public Vocabulary getVocabularyWithAllRelateds(long vocabularyId) {
		Vocabulary vocabulary = getHibernateTemplate().get(Vocabulary.class,
				vocabularyId);
		getHibernateTemplate().initialize(vocabulary.getRelatedsVocabulary());
		getHibernateTemplate().initialize(vocabulary.getVocabularyHomophones());
		getHibernateTemplate().initialize(vocabulary.getVocabularySynonyms());
		getHibernateTemplate().initialize(vocabulary.getVocabularyAntonyms());
		return vocabulary;
	}
	
}
